#!/bin/bash

set -ex

#
# find . -type f -name "_*"
#
function hack_files() {
    for f in $(find $1 -type f -name "_*"); do
        name=$(basename $f)
        updir=$(dirname $f)
        new_path=$updir/${name:1}
        echo "move $f $new_path"
        mv $f $new_path
    done
}

#
# find . -type d -name "_*"
#
function hack_dirs() {
    for d in $(find $1 -mindepth 1 -maxdepth 1 -type d); do
        if [[ ! -d $d ]]; then
            continue
        fi
        name=$(basename $d)
        if [[ ${name:0:1} == "_" ]]; then
            updir=$(dirname $d)
            new_path=$updir/${name:1}
            echo "move $d $new_path"
            mv $d $new_path
            hack_dirs $new_path
            continue
        fi
        hack_dirs $d
    done
}

pushd $1
hack_files .
hack_dirs .
popd
